//
//  NFViewController.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/7/27.
//  Copyright (c) 2015年 szb. All rights reserved.
//
#define BPNFImageCount 4

#import "NFViewController.h"
#import "BPHomeViewController.h"
#import "BPNavigationController.h"

@interface NFViewController ()<UIScrollViewDelegate>
@property (nonatomic,weak)UIPageControl *pageControl;

@end

@implementation NFViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置滑动界面
    [self setupScrollView];
    
    //[self setupPageControl];
    // Do any additional setup after loading the view.
}

- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    CGFloat imageW = scrollView.width;
    CGFloat imageH = scrollView.height;
    for (int i = 0; i < BPNFImageCount; i ++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        NSString *picname = [NSString stringWithFormat:@"new_feature_%d",i + 1];
        imageView.image = [UIImage imageNamed:picname];
        [scrollView addSubview:imageView];
        imageView.y = 0;
        imageView.width = imageW;
        imageView.height = imageH;
        imageView.x = i * imageW;
        //最后一张图片添加按钮
        if (i == BPNFImageCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
    scrollView.contentSize = CGSizeMake(BPNFImageCount * imageW, 0);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
}

- (void)setupPageControl
{
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = BPNFImageCount;
    pageControl.centerX = self.view.width * 0.5;
    pageControl.centerY = self.view.height - 30;
    [self.view addSubview:pageControl];
    pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl = pageControl;
}

- (void)setupLastImageView:(UIImageView *)imageView
{
    imageView.userInteractionEnabled = YES;
    //添加进入按钮
    [self setupStartButton:imageView];
}

- (void)setupStartButton:(UIImageView *)imageView
{
    UIButton *startButton = [[UIButton alloc] init];
    [imageView addSubview:startButton];
    
    [startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    startButton.size = startButton.currentBackgroundImage.size;
    startButton.centerX = self.view.width * 0.5;
    startButton.centerY = BPScreenH - 30.0f - startButton.height / 2.0;
    [startButton addTarget:self action:@selector(enterApp) forControlEvents:UIControlEventTouchUpInside];
}

- (void)enterApp
{
    BPHomeViewController *home = [[BPHomeViewController alloc] init];
    BPNavigationController *nav = [[BPNavigationController alloc] initWithRootViewController:home];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = nav;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat page = scrollView.contentOffset.x/scrollView.width;
    int intPage = (int)(page + 0.5);
    if (intPage == BPNFImageCount - 1) {
        [self.pageControl setHidden:YES];
    }
    self.pageControl.currentPage = intPage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
